package org.logsin37.tool.eurekamanager.api.controller;

import lombok.AllArgsConstructor;
import org.logsin37.tool.eurekamanager.app.service.EurekaService;
import org.logsin37.tool.eurekamanager.domain.entity.Eureka;
import org.logsin37.tool.eurekamanager.infra.repository.EurekaRepository;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Eureka Register Controller
 *
 * @author logsin37 2020/01/20 17:03
 */
@RestController
@RequestMapping("/eureka/manage/register")
@AllArgsConstructor
public class EurekaRegisterController {

    private final EurekaRepository eurekaRepository;
    private final EurekaService eurekaService;

    /**
     * 查询所有已保存的Eureka信息
     *
     * @return 所有已保存的Eureka信息
     */
    @GetMapping
    public Flux<Eureka> getAllRegisterEureka(){
        return this.eurekaRepository.listAllEureka();
    }

    /**
     * 根据Eureka Code查询Eureka信息
     *
     * @param code Eureka Code
     * @return Eureka信息
     */
    @GetMapping("/select-by-code")
    public Mono<Eureka> getEurekaInfoByCode(@RequestParam String code){
        return this.eurekaRepository.getEurekaInfoByCode(code);
    }

    /**
     * 保存Eureka信息
     *
     * @param eureka eureka信息
     * @return 操作结果
     */
    @PostMapping
    public Mono<Void> registerEureka(@RequestBody Eureka eureka){
        return this.eurekaService.saveEureka(eureka);
    }

    /**
     * 根据Eureka Code 移除Eureka信息
     *
     * @param code Eureka Code
     * @return 操作结果
     */
    @DeleteMapping
    public Mono<Void> removeEurekaByCode(@RequestParam String code){
        return this.eurekaService.removeEurekaByCode(code);
    }

}
